﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using Demo.Mmose.Core.Common;
using Demo.Mmose.Core.Creature;
using Demo.Mmose.Core.Item;
#endregion

namespace Demo.Mmose.Core.Map.Suit
{
    /// <summary>
    /// 
    /// </summary>
    public class GeneralPartitionSpace : IPartitionSpace
    {
        #region zh-CHS 共有常量 | en Public Constants
        /// <summary>
        /// 
        /// </summary>
        public const int MAX_NUMBER_OF_GRIDS = 64;

        /// <summary>
        /// 
        /// </summary>
        public const float SIZE_OF_GRIDS = 533.33333f;

        /// <summary>
        /// 
        /// </summary>
        public const int CENTER_GRID_ID = ( MAX_NUMBER_OF_GRIDS / 2 ); // 32

        /// <summary>
        /// 
        /// </summary>
        public const float CENTER_GRID_OFFSET = ( SIZE_OF_GRIDS / 2 ); // 266.66666f

        /// <summary>
        /// 
        /// </summary>
        public const int MAX_NUMBER_OF_CELLS = 8;

        /// <summary>
        /// 
        /// </summary>
        public const float SIZE_OF_GRID_CELL = ( SIZE_OF_GRIDS / MAX_NUMBER_OF_CELLS ); // 66.666666f

        /// <summary>
        /// 
        /// </summary>
        const int CENTER_GRID_CELL_ID = 256;

        /// <summary>
        /// 
        /// </summary>
        const float CENTER_GRID_CELL_OFFSET = ( SIZE_OF_GRID_CELL / 2 ); // 33.333333f

        /// <summary>
        /// 
        /// </summary>
        const int TOTAL_NUMBER_OF_CELLS_PER_MAP = ( MAX_NUMBER_OF_GRIDS * MAX_NUMBER_OF_CELLS ); // 512

        /// <summary>
        /// 
        /// </summary>
        const int MAP_RESOLUTION = 256;

        /// <summary>
        /// 
        /// </summary>
        const float MAP_SIZE = ( SIZE_OF_GRIDS * MAX_NUMBER_OF_GRIDS ); // 34134.0f

        /// <summary>
        /// 
        /// </summary>
        const float MAP_HALFSIZE = ( MAP_SIZE / 2 ); // 17067.0f

        /// <summary>
        /// 
        /// </summary>
        public const int MIN_GRID_DELAY = 60 * 1000;
        #endregion

        #region zh-CHS IPartitionSpace接口实现 | en Interface Implementation

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        public void ProcessPartitionSpace( object sender, PartitionSpaceEventArgs eventArgs )
        {
            eventArgs.PartitionSpace = new MapSpaceNode[MAX_NUMBER_OF_GRIDS, MAX_NUMBER_OF_GRIDS, 0];

            for ( int iIndexX = 0; iIndexX < MAX_NUMBER_OF_GRIDS; iIndexX++ )
            {
                for ( int iIndexY = 0; iIndexY < MAX_NUMBER_OF_GRIDS; iIndexY++ )
                {
                    Point3D startPoint3D = new Point3D();
                    startPoint3D.X = eventArgs.Map.MapRectangle.Start.X + ( iIndexX * SIZE_OF_GRIDS );
                    startPoint3D.Y = eventArgs.Map.MapRectangle.Start.Y + ( iIndexY * SIZE_OF_GRIDS );
                    startPoint3D.Z = eventArgs.Map.MapRectangle.Start.Z;

                    Point3D endPoint3D = new Point3D();
                    endPoint3D.X = startPoint3D.X + SIZE_OF_GRIDS;
                    endPoint3D.Y = startPoint3D.Y + SIZE_OF_GRIDS;
                    endPoint3D.Z = eventArgs.Map.MapRectangle.End.Z;

                    eventArgs.PartitionSpace[iIndexX, iIndexY, 0] = new MapSpaceNode( new Rectangle3D( startPoint3D, endPoint3D ), eventArgs.Map );
                }
            }
        }

        #region zh-CHS 私有方法 | en Private Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iGridCellPointX"></param>
        /// <param name="iGridCellPointY"></param>
        /// <param name="map"></param>
        /// <returns></returns>
        private MapSpaceNode GetSubSpaceNode( int iGridCellPointX, int iGridCellPointY, BaseMap map )
        {
            if ( iGridCellPointX < 0 || iGridCellPointY < 0 || map == null )
                return null;

            int iGridPointX = (int)( iGridCellPointX / MAX_NUMBER_OF_CELLS );
            int iGridPointY = (int)( iGridCellPointY / MAX_NUMBER_OF_CELLS );

            if ( iGridPointX >= MAX_NUMBER_OF_GRIDS || iGridPointY >= MAX_NUMBER_OF_GRIDS )
                return null;

            int iCellPointX = (int)( iGridCellPointX % MAX_NUMBER_OF_CELLS );
            int iCellPointY = (int)( iGridCellPointY % MAX_NUMBER_OF_CELLS );

            return map.SpaceNodes[iGridPointX, iGridPointY, 0].SpaceNodes[iCellPointX, iCellPointY, 0];
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        public void GetSpaceNod( object sender, GetSpaceNodeEventArgs eventArgs )
        {
            Point3D point3D = eventArgs.Map.Bound( eventArgs.X, eventArgs.Y, eventArgs.Z );
            Point3D startPoint3D = eventArgs.Map.MapRectangle.Start;

            // 获取区域点
            float fGridOffsetX = eventArgs.Map.MapRectangle.Width - ( point3D.X - startPoint3D.X );
            int iGridCellPointX = (int)( fGridOffsetX / SIZE_OF_GRID_CELL );

            float fGridOffsetY = eventArgs.Map.MapRectangle.Height - ( point3D.Y - startPoint3D.Y );
            int iGridCellPointY = (int)( fGridOffsetY / SIZE_OF_GRID_CELL );
            
            // 返回需要的区域点
            eventArgs.SpaceNode = GetSubSpaceNode( iGridCellPointX, iGridCellPointY, eventArgs.Map );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        public void GetSpaceNodesInRange( object sender, GetGetSpaceNodesInRangeEventArgs eventArgs )
        {
            List<MapSpaceNode> mapSpaceNodeList = new List<MapSpaceNode>();

            Point3D point3D = eventArgs.Map.Bound( eventArgs.X, eventArgs.Y, eventArgs.Z );
            Point3D startPoint3D = eventArgs.Map.MapRectangle.Start;

            // 获取区域点
            float fGridOffsetX = eventArgs.Map.MapRectangle.Width - ( point3D.X - startPoint3D.X );
            int iGridCellPointX = (int)( fGridOffsetX / SIZE_OF_GRID_CELL );

            float fGridOffsetY = eventArgs.Map.MapRectangle.Height - ( point3D.Y - startPoint3D.Y );
            int iGridCellPointY = (int)( fGridOffsetY / SIZE_OF_GRID_CELL );

            // 计算有多少Cell在Range内的数量
            int iCellCount = (int)( eventArgs.Range / SIZE_OF_GRID_CELL );

            // 再加上额外的计算得到正偏移的数量
            int iPlusCountX = iCellCount + ( ( eventArgs.Range % SIZE_OF_GRID_CELL ) > ( SIZE_OF_GRID_CELL - ( fGridOffsetX % SIZE_OF_GRID_CELL ) ) ? 1 : 0 );
            int iPlusCountY = iCellCount + ( ( eventArgs.Range % SIZE_OF_GRID_CELL ) > ( SIZE_OF_GRID_CELL - ( fGridOffsetY % SIZE_OF_GRID_CELL ) ) ? 1 : 0 );

            // 获取正偏移的Cell空间节点(MapSpaceNode)
            for ( int iPlusOffsetX = 0; iPlusOffsetX < iPlusCountX; iPlusOffsetX++ )
            {
                for ( int iPlusOffsetY = 0; iPlusOffsetY < iPlusCountY; iPlusOffsetY++ )
                {
                    MapSpaceNode getSubSpaceNode = GetSubSpaceNode( iGridCellPointX + iPlusOffsetX, iGridCellPointY + iPlusOffsetY, eventArgs.Map );
                    if ( getSubSpaceNode == null )
                        continue;
                    else
                        mapSpaceNodeList.Add( getSubSpaceNode );
                }
            }

            // 再加上额外的计算得到负偏移的数量
            int iMinusX = iCellCount + ( ( eventArgs.Range % SIZE_OF_GRIDS ) > ( fGridOffsetX % SIZE_OF_GRIDS ) ? 1 : 0 );
            int iMinusY = iCellCount + ( ( eventArgs.Range % SIZE_OF_GRIDS ) > ( fGridOffsetY % SIZE_OF_GRIDS ) ? 1 : 0 );

            // 获取负偏移的Cell空间节点(MapSpaceNode)
            for ( int iMinusOffsetX = 0; iMinusOffsetX < iPlusCountX; iMinusOffsetX++ )
            {
                for ( int iMinusOffsetY = 0; iMinusOffsetY < iPlusCountY; iMinusOffsetY++ )
                {
                    MapSpaceNode getSubSpaceNode = GetSubSpaceNode( iGridCellPointX - iMinusOffsetX, iGridCellPointY - iMinusOffsetY, eventArgs.Map );
                    if ( getSubSpaceNode == null )
                        continue;
                    else
                        mapSpaceNodeList.Add( getSubSpaceNode );
                }
            }

            eventArgs.SpaceNodes = mapSpaceNodeList.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        public void ActivateSpaceNodeOnMap( object sender, ActivateSpaceNodeOnMapEventArgs eventArgs )
        {
            if ( eventArgs.ActivateSpaceNode.SpaceNodeState == SpaceNodeState.Activate )
                return;

            // ActivateSpaceNode4
            MapSpaceNode getSubSpaceNode4 = GetSubSpaceNode( eventArgs.ActivateSpaceNode.RankIndex.Rank0Index - 1, eventArgs.ActivateSpaceNode.RankIndex.Rank1Index, eventArgs.ActivateSpaceNode.Owner );
            if ( getSubSpaceNode4 != null )
            {
                if ( getSubSpaceNode4.SpaceNodeState == SpaceNodeState.Deactivate )
                {
                    getSubSpaceNode4.SpaceNodeState = SpaceNodeState.HalfActivate;
                    getSubSpaceNode4.OnActivate();
                }
            }

            // ActivateSpaceNode6
            MapSpaceNode getSubSpaceNode6 = GetSubSpaceNode( eventArgs.ActivateSpaceNode.RankIndex.Rank0Index + 1, eventArgs.ActivateSpaceNode.RankIndex.Rank1Index, eventArgs.ActivateSpaceNode.Owner );
            if ( getSubSpaceNode6 != null )
            {
                if ( getSubSpaceNode6.SpaceNodeState == SpaceNodeState.Deactivate )
                {
                    getSubSpaceNode6.SpaceNodeState = SpaceNodeState.HalfActivate;
                    getSubSpaceNode6.OnActivate();
                }
            }

            // ActivateSpaceNode2
            MapSpaceNode getSubSpaceNode2 = GetSubSpaceNode( eventArgs.ActivateSpaceNode.RankIndex.Rank0Index, eventArgs.ActivateSpaceNode.RankIndex.Rank1Index - 1, eventArgs.ActivateSpaceNode.Owner );
            if ( getSubSpaceNode2 != null )
            {
                if ( getSubSpaceNode2.SpaceNodeState == SpaceNodeState.Deactivate )
                {
                    getSubSpaceNode2.SpaceNodeState = SpaceNodeState.HalfActivate;
                    getSubSpaceNode2.OnActivate();
                }
            }

            // ActivateSpaceNode8
            MapSpaceNode getSubSpaceNode8 = GetSubSpaceNode( eventArgs.ActivateSpaceNode.RankIndex.Rank0Index, eventArgs.ActivateSpaceNode.RankIndex.Rank1Index + 1, eventArgs.ActivateSpaceNode.Owner );
            if ( getSubSpaceNode8 != null )
            {
                if ( getSubSpaceNode8.SpaceNodeState == SpaceNodeState.Deactivate )
                {
                    getSubSpaceNode8.SpaceNodeState = SpaceNodeState.HalfActivate;
                    getSubSpaceNode8.OnActivate();
                }
            }

            // ActivateSpaceNode1
            MapSpaceNode getSubSpaceNode1 = GetSubSpaceNode( eventArgs.ActivateSpaceNode.RankIndex.Rank0Index - 1, eventArgs.ActivateSpaceNode.RankIndex.Rank1Index - 1, eventArgs.ActivateSpaceNode.Owner );
            if ( getSubSpaceNode1 != null )
            {
                if ( getSubSpaceNode1.SpaceNodeState == SpaceNodeState.Deactivate )
                {
                    getSubSpaceNode1.SpaceNodeState = SpaceNodeState.HalfActivate;
                    getSubSpaceNode1.OnActivate();
                }
            }

            // ActivateSpaceNode3
            MapSpaceNode getSubSpaceNode3 = GetSubSpaceNode( eventArgs.ActivateSpaceNode.RankIndex.Rank0Index + 1, eventArgs.ActivateSpaceNode.RankIndex.Rank1Index - 1, eventArgs.ActivateSpaceNode.Owner );
            if ( getSubSpaceNode3 != null )
            {
                if ( getSubSpaceNode3.SpaceNodeState == SpaceNodeState.Deactivate )
                {
                    getSubSpaceNode3.SpaceNodeState = SpaceNodeState.HalfActivate;
                    getSubSpaceNode3.OnActivate();
                }
            }

            // ActivateSpaceNode7
            MapSpaceNode getSubSpaceNode7 = GetSubSpaceNode( eventArgs.ActivateSpaceNode.RankIndex.Rank0Index - 1, eventArgs.ActivateSpaceNode.RankIndex.Rank1Index + 1, eventArgs.ActivateSpaceNode.Owner );
            if ( getSubSpaceNode7 != null )
            {
                if ( getSubSpaceNode7.SpaceNodeState == SpaceNodeState.Deactivate )
                {
                    getSubSpaceNode7.SpaceNodeState = SpaceNodeState.HalfActivate;
                    getSubSpaceNode7.OnActivate();
                }
            }

            // ActivateSpaceNode9
            MapSpaceNode getSubSpaceNode9 = GetSubSpaceNode( eventArgs.ActivateSpaceNode.RankIndex.Rank0Index + 1, eventArgs.ActivateSpaceNode.RankIndex.Rank1Index + 1, eventArgs.ActivateSpaceNode.Owner );
            if ( getSubSpaceNode9 != null )
            {
                if ( getSubSpaceNode9.SpaceNodeState == SpaceNodeState.Deactivate )
                {
                    getSubSpaceNode9.SpaceNodeState = SpaceNodeState.HalfActivate;
                    getSubSpaceNode9.OnActivate();
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        public void DeactivateSpaceNodeOnMap( object sender, DeactivateSpaceNodeOnMapEventArgs eventArgs )
        {
            if ( eventArgs.DeactivateSpaceNode.SpaceNodeState != SpaceNodeState.Activate )
                return;

            // 初始化
            bool isDeactivateSpaceNode1 = true;
            bool isDeactivateSpaceNode2 = true;
            bool isDeactivateSpaceNode3 = true;
            bool isDeactivateSpaceNode4 = true;
            bool isDeactivateSpaceNode6 = true;
            bool isDeactivateSpaceNode7 = true;
            bool isDeactivateSpaceNode8 = true;
            bool isDeactivateSpaceNode9 = true;

            MapSpaceNode getSubSpaceNode4 = null;
            if ( isDeactivateSpaceNode4 == true )
            {
                getSubSpaceNode4 = GetSubSpaceNode( eventArgs.DeactivateSpaceNode.RankIndex.Rank0Index - 1, eventArgs.DeactivateSpaceNode.RankIndex.Rank1Index, eventArgs.DeactivateSpaceNode.Owner );
                if ( getSubSpaceNode4 != null )
                {
                    if ( getSubSpaceNode4.SpaceNodeState == SpaceNodeState.Activate )
                    {
                        isDeactivateSpaceNode1 = false;
                        isDeactivateSpaceNode2 = false;
                        isDeactivateSpaceNode7 = false;
                        isDeactivateSpaceNode8 = false;
                    }
                }
            }

            MapSpaceNode getSubSpaceNode6 = null;
            if ( isDeactivateSpaceNode6 == true )
            {
                getSubSpaceNode6 = GetSubSpaceNode( eventArgs.DeactivateSpaceNode.RankIndex.Rank0Index + 1, eventArgs.DeactivateSpaceNode.RankIndex.Rank1Index, eventArgs.DeactivateSpaceNode.Owner );
                if ( getSubSpaceNode6 != null )
                {
                    if ( getSubSpaceNode6.SpaceNodeState == SpaceNodeState.Activate )
                    {
                        isDeactivateSpaceNode2 = false;
                        isDeactivateSpaceNode3 = false;
                        isDeactivateSpaceNode8 = false;
                        isDeactivateSpaceNode9 = false;
                    }
                }
            }

            MapSpaceNode getSubSpaceNode2 = null;
            if ( isDeactivateSpaceNode2 == true )
            {
                getSubSpaceNode2 = GetSubSpaceNode( eventArgs.DeactivateSpaceNode.RankIndex.Rank0Index, eventArgs.DeactivateSpaceNode.RankIndex.Rank1Index - 1, eventArgs.DeactivateSpaceNode.Owner );
                if ( getSubSpaceNode2 != null )
                {
                    if ( getSubSpaceNode2.SpaceNodeState == SpaceNodeState.Activate )
                    {
                        isDeactivateSpaceNode1 = false;
                        isDeactivateSpaceNode3 = false;
                        isDeactivateSpaceNode4 = false;
                        isDeactivateSpaceNode6 = false;
                    }
                }
            }

            MapSpaceNode getSubSpaceNode8 = null;
            if ( isDeactivateSpaceNode8 == true )
            {
                getSubSpaceNode8 = GetSubSpaceNode( eventArgs.DeactivateSpaceNode.RankIndex.Rank0Index, eventArgs.DeactivateSpaceNode.RankIndex.Rank1Index + 1, eventArgs.DeactivateSpaceNode.Owner );
                if ( getSubSpaceNode8 != null )
                {
                    if ( getSubSpaceNode8.SpaceNodeState == SpaceNodeState.Activate )
                    {
                        isDeactivateSpaceNode4 = false;
                        isDeactivateSpaceNode6 = false;
                        isDeactivateSpaceNode7 = false;
                        isDeactivateSpaceNode9 = false;
                    }
                }
            }

            MapSpaceNode getSubSpaceNode1 = null;
            if ( isDeactivateSpaceNode1 == true )
            {
                getSubSpaceNode1 = GetSubSpaceNode( eventArgs.DeactivateSpaceNode.RankIndex.Rank0Index - 1, eventArgs.DeactivateSpaceNode.RankIndex.Rank1Index - 1, eventArgs.DeactivateSpaceNode.Owner );
                if ( getSubSpaceNode1 != null )
                {
                    if ( getSubSpaceNode1.SpaceNodeState == SpaceNodeState.Activate )
                    {
                        isDeactivateSpaceNode2 = false;
                        isDeactivateSpaceNode4 = false;
                    }
                }
            }

            MapSpaceNode getSubSpaceNode3 = null;
            if ( isDeactivateSpaceNode3 == true )
            {
                getSubSpaceNode3 = GetSubSpaceNode( eventArgs.DeactivateSpaceNode.RankIndex.Rank0Index + 1, eventArgs.DeactivateSpaceNode.RankIndex.Rank1Index - 1, eventArgs.DeactivateSpaceNode.Owner );
                if ( getSubSpaceNode3 != null )
                {
                    if ( getSubSpaceNode3.SpaceNodeState == SpaceNodeState.Activate )
                    {
                        isDeactivateSpaceNode2 = false;
                        isDeactivateSpaceNode6 = false;
                    }
                }
            }

            MapSpaceNode getSubSpaceNode7 = null;
            if ( isDeactivateSpaceNode7 == true )
            {
                getSubSpaceNode7 = GetSubSpaceNode( eventArgs.DeactivateSpaceNode.RankIndex.Rank0Index - 1, eventArgs.DeactivateSpaceNode.RankIndex.Rank1Index + 1, eventArgs.DeactivateSpaceNode.Owner );
                if ( getSubSpaceNode7 != null )
                {
                    if ( getSubSpaceNode7.SpaceNodeState == SpaceNodeState.Activate )
                    {
                        isDeactivateSpaceNode4 = false;
                        isDeactivateSpaceNode8 = false;
                    }
                }
            }

            MapSpaceNode getSubSpaceNode9 = null;
            if ( isDeactivateSpaceNode9 == true )
            {
                getSubSpaceNode9 = GetSubSpaceNode( eventArgs.DeactivateSpaceNode.RankIndex.Rank0Index + 1, eventArgs.DeactivateSpaceNode.RankIndex.Rank1Index + 1, eventArgs.DeactivateSpaceNode.Owner );
                if ( getSubSpaceNode9 != null )
                {
                    if ( getSubSpaceNode9.SpaceNodeState == SpaceNodeState.Activate )
                    {
                        isDeactivateSpaceNode6 = false;
                        isDeactivateSpaceNode8 = false;
                    }
                }
            }

            // 再次放入列表检查
            if ( isDeactivateSpaceNode1 == true && getSubSpaceNode1 != null )
            {
                if ( getSubSpaceNode1.SpaceNodeState == SpaceNodeState.HalfActivate )
                {
                    // 放入列表检查
                    //getSubSpaceNode1.OnDeactivate();
                }
            }

            if ( isDeactivateSpaceNode2 == true && getSubSpaceNode2 != null )
            {
                if ( getSubSpaceNode2.SpaceNodeState == SpaceNodeState.HalfActivate )
                {
                    // 放入列表检查
                    //getSubSpaceNode2.OnDeactivate();
                }
            }

            if ( isDeactivateSpaceNode3 == true && getSubSpaceNode3 != null )
            {
                if ( getSubSpaceNode3.SpaceNodeState == SpaceNodeState.HalfActivate )
                {
                    // 放入列表检查
                    //getSubSpaceNode3.OnDeactivate();
                }
            }

            if ( isDeactivateSpaceNode4 == true && getSubSpaceNode4 != null )
            {
                if ( getSubSpaceNode4.SpaceNodeState == SpaceNodeState.HalfActivate )
                {
                    // 放入列表检查
                    //getSubSpaceNode4.OnDeactivate();
                }
            }

            if ( isDeactivateSpaceNode6 == true && getSubSpaceNode6 != null )
            {
                if ( getSubSpaceNode6.SpaceNodeState == SpaceNodeState.HalfActivate )
                {
                    // 放入列表检查
                    //getSubSpaceNode6.OnDeactivate();
                }
            }

            if ( isDeactivateSpaceNode7 == true && getSubSpaceNode7 != null )
            {
                if ( getSubSpaceNode7.SpaceNodeState == SpaceNodeState.HalfActivate )
                {
                    // 放入列表检查
                    //getSubSpaceNode7.OnDeactivate();
                }
            }

            if ( isDeactivateSpaceNode8 == true && getSubSpaceNode8 != null )
            {
                if ( getSubSpaceNode8.SpaceNodeState == SpaceNodeState.HalfActivate )
                {
                    // 放入列表检查
                    //getSubSpaceNode8.OnDeactivate();
                }
            }

            if ( isDeactivateSpaceNode9 == true && getSubSpaceNode9 != null )
            {
                if ( getSubSpaceNode9.SpaceNodeState == SpaceNodeState.HalfActivate )
                {
                    // 放入列表检查
                    //getSubSpaceNode9.OnDeactivate();
                }
            }

            MapSpaceNode getSubSpaceNode5 = eventArgs.DeactivateSpaceNode;
            getSubSpaceNode5.SpaceNodeState = SpaceNodeState.HalfActivate;

            // 自己
            if ( isDeactivateSpaceNode1 == true &&
                isDeactivateSpaceNode2 == true &&
                isDeactivateSpaceNode3 == true &&
                isDeactivateSpaceNode4 == true &&
                isDeactivateSpaceNode6 == true &&
                isDeactivateSpaceNode7 == true &&
                isDeactivateSpaceNode8 == true &&
                isDeactivateSpaceNode9 == true )
            {
                // 放入列表检查
                //getSubSpaceNode5.OnDeactivate();
            }
        }

        #endregion

        #region zh-CHS IPartitionSpaceNode接口实现 | en Interface Implementation

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        public void ProcessPartitionSpaceNode( object sender, PartitionSpaceNodeEventArgs eventArgs )
        {
            if ( eventArgs.ParentSpaceNode.LayerIndex != 0 )
                return;

            eventArgs.PartitionSpace = new MapSpaceNode[MAX_NUMBER_OF_CELLS, MAX_NUMBER_OF_CELLS, 0];

            for ( int iIndexX = 0; iIndexX < MAX_NUMBER_OF_CELLS; iIndexX++ )
            {
                for ( int iIndexY = 0; iIndexY < MAX_NUMBER_OF_CELLS; iIndexY++ )
                {
                    Point3D startPoint3D = new Point3D();
                    startPoint3D.X = eventArgs.ParentSpaceNode.SpaceNodeRectangle.Start.X + ( iIndexX * SIZE_OF_GRID_CELL );
                    startPoint3D.Y = eventArgs.ParentSpaceNode.SpaceNodeRectangle.Start.Y + ( iIndexY * SIZE_OF_GRID_CELL );
                    startPoint3D.Z = eventArgs.ParentSpaceNode.SpaceNodeRectangle.Start.Z;

                    Point3D endPoint3D = new Point3D();
                    endPoint3D.X = startPoint3D.X + SIZE_OF_GRID_CELL;
                    endPoint3D.Y = startPoint3D.Y + SIZE_OF_GRID_CELL;
                    endPoint3D.Z = eventArgs.ParentSpaceNode.SpaceNodeRectangle.End.Z;

                    eventArgs.PartitionSpace[iIndexX, iIndexY, 0] = new MapSpaceNode( new Rectangle3D( startPoint3D, endPoint3D ), eventArgs.Map );

                    eventArgs.PartitionSpace[iIndexX, iIndexY, 0].ThreadCharacterEnterSpaceNode += new EventHandler<CharacterEnterSpaceNodeEventArgs>( this.PlayerEnterSpaceNode );
                    eventArgs.PartitionSpace[iIndexX, iIndexY, 0].ThreadCharacterLeaveSpaceNode += new EventHandler<CharacterLeaveSpaceNodeEventArgs>( this.PlayerLeaveSpaceNode );
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        private void PlayerEnterSpaceNode( object sender, CharacterEnterSpaceNodeEventArgs eventArgs )
        {
            if ( eventArgs.SpaceNode.PlayersCount > 0 && eventArgs.SpaceNode.SpaceNodeState != SpaceNodeState.Activate )　// 如果游戏玩家的列表是空的话,则激活区域点,说明此区域点有玩家存在
                eventArgs.SpaceNode.Owner.OnActivateSpaceNode( eventArgs.SpaceNode );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        private void PlayerLeaveSpaceNode( object sender, CharacterLeaveSpaceNodeEventArgs eventArgs )
        {
            if ( eventArgs.SpaceNode.PlayersCount <= 0 && eventArgs.SpaceNode.SpaceNodeState == SpaceNodeState.Activate )　// 如果游戏玩家的列表是空的话,则无效区域点,说明此区域点玩家不存在
                eventArgs.SpaceNode.Owner.OnDeactivateSpaceNode( eventArgs.SpaceNode );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        public void ActivateSpaceNode( object sender, ActivateSpaceNodeEventArgs eventArgs )
        {
            BaseCreature[] creatureArray = eventArgs.SpaceNode.CreaturesToArray();

            foreach ( BaseCreature baseCreature in creatureArray )
                baseCreature.OnSpaceNodeActivate();

            BaseItem[] itemArray = eventArgs.SpaceNode.ItemsToArray();

            foreach ( BaseItem baseItem in itemArray )
                baseItem.OnSpaceNodeDeactivate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        public void DeactivateSpaceNode( object sender, DeactivateSpaceNodeEventArgs eventArgs )
        {
            if (eventArgs.SpaceNode.SpaceNodeState != SpaceNodeState.Deactivate)
                return;

            BaseCreature[] creatureArray = eventArgs.SpaceNode.CreaturesToArray();

            foreach ( BaseCreature baseCreature in creatureArray )
            {
                // 在多线程中的检测，用于中断操作
                if ( eventArgs.SpaceNode.SpaceNodeState != SpaceNodeState.Deactivate )
                    return;

                baseCreature.OnSpaceNodeDeactivate();
            }

            BaseItem[] itemArray = eventArgs.SpaceNode.ItemsToArray();

            foreach ( BaseItem baseItem in itemArray )
            {
                // 在多线程中的检测，用于中断操作
                if ( eventArgs.SpaceNode.SpaceNodeState != SpaceNodeState.Deactivate )
                    return;

                baseItem.OnSpaceNodeDeactivate();
            }
        }

        #endregion
    }
}
#endregion